﻿using DevicesView.Entity.ViewData;
using DevicesView.Logic.Print;
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using DevicesView.Logic.Test;
using Devices;

namespace DevicesView.Config
{
    /// <summary>
    /// UCPrintItem.xaml 的交互逻辑
    /// </summary>
    public partial class UCPrintItem : UserControl
    {

        private Devices.DevicesInformation devices;
        private Devices.PrintInfo print;
        public UCPrintItem()
        {
            InitializeComponent();
        }

        public UCPrintItem(Devices.IDevices _device)
        {
            InitializeComponent();
            devices = _device.Info;
            print = _device.PrintTool?.PrintInfo;
        }

        public UCPrintItem(Devices.DevicesInformation _info)
        {
            InitializeComponent();
            devices = _info;

        }






        private void Combobox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            print = Combobox1.SelectedItem as PrintInfo;
            PrintLogic.SavePrintConfig(devices,print);
            RefreshView();
        }


        private void RefreshView()
        {
            labelDescribe.Text = print.Describe;
            if (print.Images != null)
                pictureBox1.Source = PrintLogic.GetBitImage(print.Images[0]);
        }

        private void PictureBox1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var testResults = new ResultLogic().GetResultById(46);
            if (testResults != null)
            {
                IDevices dev = DevicesCollection.CreateDevices("print\\temp.xml", testResults.Info.ClassInfo);
                dev.PrintTool = PrintLogic.GetPrintConfig(devices);
                dev.PrintTool.Preview(new List<Result> { testResults });
            }
        }

        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (devices == null)
                return;

            List<PrintInfo> pinfos = PrintLogic.GetPrints();
            this.Combobox1.ItemsSource = pinfos;
            Combobox1.DisplayMemberPath = "Name";
            groupControl1.Text = TextDevies.Text = devices.DisplayName;

            if (print == null)
                print = PrintLogic.GetPrintConfig(devices)?.PrintInfo;

            Combobox1.SelectedItem = pinfos.FirstOrDefault(o => o.Name == print.Name);
            RefreshView();
            Combobox1.SelectionChanged += Combobox1_SelectionChanged;
        }
    }
}
